package com.example.footballsystem.service;

import com.example.footballsystem.entity.Comment;
import com.example.footballsystem.entity.Post;
import com.example.footballsystem.mapper.LikeMapper;
import com.example.footballsystem.mapper.PostMapper;
import com.example.footballsystem.mapper.CommentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class PostService {
    @Autowired
    PostMapper newsMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserService userService;
    @Autowired
    LikeMapper likeMapper;
    //添加新闻
    public Boolean addNew(Post news){
        Timestamp create_date = new Timestamp(System.currentTimeMillis());
        news.setCreateDate(create_date.toString());
        log.info(news.toString());
        news.setNewID(newsMapper.maxNewID()+1);
        news.setImagelink("https://img01.scbao.com/160731/234547-160I100532071.jpg");
        return newsMapper.addNew(news);
    }
    public List<Post> getAllPost(String account){
        List<Integer> postLisk = likeMapper.getLikeByUserID(account);
        List<Post> allPost = newsMapper.getAllNews();
        for(int i=0;i<allPost.size();i++){
            //该帖子被喜欢
            if(postLisk.contains(allPost.get(i).getNewID())){
                allPost.get(i).setLike(true);
            }else{
                allPost.get(i).setLike(false);
            }
        }
        return newsMapper.getAllNews();
    }
    public Boolean deleteNew(Post news){
        return newsMapper.deleteNew(news.getNewID());
    }
    public Post getPostByID(int id){
        return  newsMapper.searchByNewID(id);
    }
    //搜索新闻
    public List<Post> searchNews(String info){
        List<Post> resultList = new ArrayList<>();
        int count = 0;

        if(!newsMapper.searchByTitle(info).isEmpty()){
            resultList.addAll((newsMapper.searchByTitle("%"+info+"%")));
            count+=newsMapper.searchByTitle("%"+info+"%").size();
        }
        log.info("1"+resultList.toString());
        try{
            if(newsMapper.searchByNewID(Integer.parseInt(info))!=null){
                resultList.add((newsMapper.searchByNewID(Integer.parseInt(info))));
                count++;//newsMapper.searchByNewID(Integer.parseInt(info)).size();
            }
            log.info("2"+resultList.toString());
        }catch (NumberFormatException e) {
            if (!newsMapper.searchByContent(info).isEmpty()) {
                resultList.addAll((newsMapper.searchByContent("%"+info+"%")));
                count+=newsMapper.searchByContent("%"+info+"%").size();
            }
            log.info("3"+resultList.toString());
            for (int i = 0; i < count; i++) {
                for (int j = i + 1; j < count; j++) {
                    if (Objects.equals(resultList.get(j).getNewID(), resultList.get(j).getNewID())) {
                        log.info(i + " " + j);
                        resultList.remove(j);
                        j--;
                        count--;
                    }
                }
            }
            log.info("4"+resultList.toString());
            //log.info(resultList.toString());
            return resultList;

        }
        log.info(Integer.toString(count));
        if(!newsMapper.searchByContent(info).isEmpty()){
            resultList.addAll((newsMapper.searchByContent("%"+info+"%")));
            count++;
        }
        log.info("5"+resultList.toString());
        int[] arr = new int[resultList.size()];
        for (int i=0;i<resultList.size();i++) {
            for (int j=i+1;j<resultList.size();j++) {
                if(Objects.equals(resultList.get(j).getNewID(), resultList.get(j).getNewID())){
                    log.info(i+" "+j);
                    arr[j]=1;
                }
            }
        }
        int num=arr.length;
        log.info(Integer.toString(arr.length));
        for(int i=0;i<num;i++){
            if(arr[i]==1){
                resultList.remove(i);
                num--;
            }
        }
//        LinkedHashSet<Post> hashSet = new LinkedHashSet<>(resultList);
//        ArrayList<Post> listWithoutDuplicates = new ArrayList<>(hashSet);
//        log.info("6"+resultList.toString());
        return resultList;
    }
    public Boolean addComment(Comment comment){
        comment.setUserimage(userService.getUser(comment.getUserAccount()).getUrl());
        newsMapper.updateCommentNum(comment.getSchoolNewID());
        return commentMapper.addComment(comment);
    }
    public Boolean addLike(String account,int postID,int num){
        if(likeMapper.getLikeByUserID(account).contains(postID)){
            return newsMapper.updateLikeNum(postID,num);
        }else{
            return likeMapper.insertLike(account,postID)&&newsMapper.updateLikeNum(postID,num);
        }

    }
    public List<Comment> getCommentByPost(int postID){
        return commentMapper.getCommentByPost(postID);
    }
    public List<Comment> getCommentByAccount(String account){
        return commentMapper.getCommentByAccount(account);
    }
    public List<Integer> getPostCount(){
        return newsMapper.countNews();
    }
    public Integer count(){
        return newsMapper.count();
    }
//    public List<Boolean> getAllLike(String account){
//        List<Boolean> resultList = new ArrayList<>();
//        List<Integer> postLisk = likeMapper.getLikeByUserID(account);
//        List<Post> allPost = getAllPost();
//        for(int i=0;i<allPost.size();i++){
//            //该帖子被喜欢
//            if(postLisk.contains(allPost.get(i).getNewID())){
//                resultList.add(true);
//            }else{
//                resultList.add(false);
//            }
//        }
//        return resultList;
//    }
}
